﻿package cn.valorin.command.sub;

import static cn.valorin.configuration.languagefile.MessageSender.sm;
import cn.valorin.Main;
import cn.valorin.command.SubCommand;
import cn.valorin.command.way.AdminCommands;
import cn.valorin.data.MySQL;

import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

public class Reload extends SubCommand implements AdminCommands{

	public Reload() {
		super("reload");
	}
	
	public void sendHelp(Player p)
	{
	  sm("",p);
	  sm("&3&lDuan&b&l&oWu &f&l>> &a管理员帮助",p,false);
      sm("&b/duanwu reload c &f- &a重载config.yml",p,false);
      sm("&b/duanwu reload l &f- &a重载所有语言文件",p,false);
      sm("",p);
	}

	@Override
	public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) 
	{
	  Player p = null;
      if (sender instanceof Player)
      { p = (Player)sender; }
      if (args.length == 1)
      {
        sendHelp(p);
      } else {
    	if (args[1].equalsIgnoreCase("c"))
    	{
    	  try {
        	long start = System.currentTimeMillis();
        	Main.getInstance().reloadConfig();
        	Main.getInstance().setPrefix(
        			Main.getInstance().getConfig().getString("Prefix").replace("&", "§"));
        	MySQL mysql = Main.getInstance().getMySQL();
        	if (mysql.isEnabled()) { mysql.close(); }
        	mysql.connect();
        	long end = System.currentTimeMillis();
        	sm("&aconfig.yml:重载完毕！耗时&d{ms}毫秒",p,"ms",new String[]{""+(end-start)});
          } catch (Exception e) {
        	  e.printStackTrace();
        	sm("&cconfig.yml:重载时发生异常！建议重启本插件(若服务器装有具有重载其他插件功能的插件如:Yum和PluginMan等)或重启服务器",p);
          }
    	  return true;
    	}
    	if (args[1].equalsIgnoreCase("l"))
    	{
    	  try {
    	    long start = System.currentTimeMillis();
    	    Main.getInstance().reloadLanguageFileLoader();
    		long end = System.currentTimeMillis();
    		sm("&aLanguage file:重载完毕！耗时&d{ms}毫秒",p,"ms",new String[]{""+(end-start)});
    	  } catch (Exception e) {
    		sm("&cLanguage file:重载时发生异常！建议重启本插件(若服务器装有具有重载其他插件功能的插件如:Yum和PluginMan等)或重启服务器",p);
    	  }
    	  return true;
    	}
      }
	  return true;
	}

}
